package com.entrogy.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author xlr
 * @description 图片转Base64
 */
public class PicUtils {

    private static Logger logger = LoggerFactory.getLogger(PicUtils.class);

    /**
     * 本地图片转换base64
     * @param base_image
     * @return
     */
    public static String ImageToBase64(String base_image) {
        String base64 = "";
        byte[] img_data;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedImage buff_img = null;
        File file = new File(base_image);

        try {
            buff_img = ImageIO.read(file);
            // 读取图片
            ImageIO.write(buff_img, "jpg", out);
            // 编码
            img_data = out.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            base64 = encoder.encode(img_data);
            base64 = base64.replaceAll("\r\n", "");
            // base64 = base64.replace("+", "%2B");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return base64;
    }

    /**
     * 远程图片转换
     * @param filePath
     * @return
     */
    public static String openFileBase64(String filePath) {
        filePath = filePath.replaceAll("\\\\","/");
        String base64 = new String();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse httpResponse = null;
        HttpGet get = new HttpGet(filePath);
        HttpEntity entity = null;
        try {
            httpResponse = httpclient.execute(get);
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                entity = httpResponse.getEntity();
                BASE64Encoder encoder = new BASE64Encoder();
                base64 = encoder.encode(EntityUtils.toByteArray(entity));
                base64 = base64.replaceAll("\r\n", "");
            }else{
                logger.info("推送闸机图片的时候远程文件无法连接");
            }
            httpResponse.close();
            httpclient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                httpResponse.close();
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return base64;
    }

}
